<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/5/11
 * Time: 11:18
 */

namespace app\admin\controller;

use app\admin\model\User as UserModel;
use app\admin\model\Userbg as UserbgModel;
use think\Db;
class UserController extends CommonController
{
    /**
     * 展示用户信息
     * @param $page  页数
     * @param $rows  每页显示的条数
     * @return string
     */
    public function userList(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = $this->getDataByCampusid($_POST);
        $res = Db::table('ew_user')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total =Db::table('ew_user')
            ->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    public function price(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
//        $where = $this->getDataByCampusid($_POST);
        $res = Db::table('ew_last')
//            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total =Db::table('ew_last')
//            ->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    
    /**
     * 展示背景图片信息
     * @param $page
     * @param $rows
     * @return string
     */
    public function userBg($page,$rows){
        $userbgList = UserbgModel::getUserbgList($page,$rows);
        $total = UserbgModel::getTotalNum();
        $data['total'] = $total;
        $data['rows'] = $userbgList;
        return json_encode($data);
    }
    
    /**
     * 展示添加页面 弹框
     * @return mixed
     */
    public function add(){
        return $this->fetch('user/add');
    }
    
    /**
     * 将上传的图片添加到文件夹和数据库中
     * @return string
     */
    public function insertBg(){
        $imginfo = $this->uploadFile($_FILES['fileImg']['tmp_name'],$_FILES['fileImg']['name'],$_FILES['fileImg']['size']);
        if($imginfo['status'] != 200){
            return json_encode(array("status" => 0, "msg" => $imginfo['msg']));
        }
        $data['bgpic'] = $imginfo['msg'];
        $res = UserbgModel::insertBg($data);//图片路径添加到数据库中
        if($res){
            return json_encode(array("status" => 1, "msg" => "添加成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "添加失败！"));
        }
    }
    
    /**
     * 删除选定的背景图片
     * @param $id
     * @return string
     */
    public function deleteBg($id){
        if(!$id == ''){
//            $url = UserbgModel::getColumn($id);
            $row = UserbgModel::deleteBg($id);
            if($row){
                return json_encode(['status'=>1, 'msg'=>'删除成功']);
            }else{
                return json_encode(['status'=>0, 'msg'=>'删除失败']);
            }
        }else{
            return json_encode(['status'=>0, 'msg'=>'参数错误']);
        }
    }
    
    /**
     * 展示修改图片页面
     * @param $id
     * @return mixed
     */
    public function updateBg($id){
        $res = Db::table('ew_userbg')->where('id',$id)->find();
        $this->assign('res',$res);
        return $this->fetch('user/update');
    }
    public function updateP($id){
        $res = Db::table('ew_last')->where('id',$id)->find();
        $this->assign('res',$res);
        return $this->fetch('user/updatepic');
    }

    /**
     * 修改图片
     * @param $id
     * @return string
     */
    public function update($id){
        if(!empty($_FILES['fileImg']['type'])){
            $imginfo = $this->uploadFile($_FILES['fileImg']['tmp_name'],$_FILES['fileImg']['name'],$_FILES['fileImg']['size']);
            if($imginfo['status'] != 200){
                return json_encode(array("status" => 0, "msg" => $imginfo['msg']));
            }
            $data['bgpic'] = $imginfo['msg'];
            $res = Db::table('ew_userbg')->where('id', $id)->update($data);
            if($res){
                return json_encode(['status'=>1, 'msg'=>'修改成功']);
            }else{
                return json_encode(['status'=>0, 'msg'=>'修改失败']);
            }
        }else{
            return json_encode(['status'=>0,'msg'=>'您未修改任何内容']);
        }
    }
    public function updatepic($id){
        if(!empty($_FILES['fileImg']['type'])){
            $imginfo = $this->uploadFile($_FILES['fileImg']['tmp_name'],$_FILES['fileImg']['name'],$_FILES['fileImg']['size']);
            if($imginfo['status'] != 200){
                return json_encode(array("status" => 0, "msg" => $imginfo['msg']));
            }
            $data['pic'] = $imginfo['msg'];
            $res = Db::table('ew_last')->where('id', $id)->update($data);
            if($res){
                return json_encode(['status'=>1, 'msg'=>'修改成功']);
            }else{
                return json_encode(['status'=>0, 'msg'=>'修改失败']);
            }
        }else{
            return json_encode(['status'=>0,'msg'=>'您未修改任何内容']);
        }
    }









    public function ips(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = $this->getDataByCampusid($_POST);
        $res = Db::table('ew_ip')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total =Db::table('ew_ip')
            ->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
}